import 'package:flutter/material.dart';

import 'package:junyeyishu/utils/utils.dart';
import 'package:junyeyishu/widgets/QGButton.dart';

class QGBatchOnshelfAlert extends StatefulWidget {
  Function(String price, String amount, String password, bool type) onConfirm;
  QGBatchOnshelfAlert({super.key, required this.onConfirm});
  @override
  State<QGBatchOnshelfAlert> createState() => _QGBatchOnshelfAlertState();

  static show(context, onConfirm) {
    showDialog(
        context: context,
        builder: (context) {
          return QGBatchOnshelfAlert(onConfirm: onConfirm);
        });
  }
}

class _QGBatchOnshelfAlertState extends State<QGBatchOnshelfAlert> {
  // bool check = false;

  // var tapGes = TapGestureRecognizer();
  var priceController = TextEditingController();
  var amountController = TextEditingController();
  var passwordController = TextEditingController();

  // double price = 0;
  bool type = true; //上架

  @override
  void dispose() {
    // tapGes.dispose();
    priceController.dispose();
    amountController.dispose();
    passwordController.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        unfocus(context);
      },
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            margin: EdgeInsets.symmetric(horizontal: 25.rpx),
            padding: EdgeInsets.symmetric(horizontal: 30.rpx, vertical: 25.rpx),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(20.rpx),
              color: Color(0xFFFFFFFF),
            ),
            child: Column(mainAxisSize: MainAxisSize.min, children: [
              Text(
                '批量操作',
                style: TextStyle(
                    color: Color(0xFF111111),
                    fontSize: 16.rpx,
                    fontWeight: FontWeight.normal),
              ),
              SizedBox(height: 10.rpx),
              type ? buildInput("价格", priceController) : Container(),
              buildInput("数量", amountController),
              type ? buildInput("密码", passwordController) : Container(),
              SizedBox(height: 8.rpx),
              buildTypes(),
              SizedBox(height: 30.rpx),
              buildButtons(),
            ]),
          ),
        ],
      ),
    );
  }

  buildInput(title, controller) {
    return Row(
      children: [
        Text(
          title,
          style: TextStyle(
              color: Color(0xFF111111),
              fontSize: 14.rpx,
              fontWeight: FontWeight.normal),
        ),
        Expanded(
          child: TextField(
              textAlign: TextAlign.end,
              controller: controller,
              keyboardType:
                  const TextInputType.numberWithOptions(decimal: true),
              textInputAction: TextInputAction.done,
              cursorColor: Color(0xFF111111),
              style: TextStyle(
                  color: Color(0xFF111111),
                  fontSize: 15.rpx,
                  fontWeight: FontWeight.normal),
              decoration: InputDecoration(
                hintText: "-",
                hintStyle:
                    TextStyle(color: Color.fromRGBO(17, 17, 17, 0.6)), //修改颜色
                enabledBorder: InputBorder.none,
                focusedBorder: InputBorder.none,
              )),
        ),
      ],
    );
  }

  buildTypes() {
    return Row(
      children: [
        Expanded(
          child: Text(
            '类型',
            style: TextStyle(
                color: Color(0xFF111111),
                fontSize: 14.rpx,
                fontWeight: FontWeight.normal),
          ),
        ),
        GestureDetector(
          onTap: () {
            type = true;
            setState(() {});
          },
          child: Container(
            padding: EdgeInsets.symmetric(vertical: 5.rpx, horizontal: 10.rpx),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(4.rpx),
              color:
                  type ? Color.fromRGBO(17, 17, 17, 0.16) : Colors.transparent,
            ),
            child: Text(
              '上架',
              style: TextStyle(
                  color: Color(0xFF111111),
                  fontSize: 12.rpx,
                  fontWeight: FontWeight.normal),
            ),
          ),
        ),
        SizedBox(width: 10.rpx),
        GestureDetector(
          onTap: () {
            type = false;
            setState(() {});
          },
          child: Container(
            padding: EdgeInsets.symmetric(vertical: 5.rpx, horizontal: 10.rpx),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(5.rpx),
              color:
                  type ? Colors.transparent : Color.fromRGBO(17, 17, 17, 0.16),
            ),
            child: Text(
              '下架',
              style: TextStyle(
                  color: Color(0xFF111111),
                  fontSize: 12.rpx,
                  fontWeight: FontWeight.normal),
            ),
          ),
        ),
      ],
    );
  }

  buildButtons() {
    return SizedBox(
      height: 40.rpx,
      child: Row(
        children: [
          Expanded(
              child:
                  QGOutlineButton(title: "取消", enable: true, onTap: onCancel)),
          SizedBox(width: 15.rpx),
          Expanded(
              child: QGButton(
                  title: "确定",
                  enable: true,
                  onTap: () {
                    unfocus(context);

                    var price = priceController.text;
                    var amount = amountController.text;
                    var password = passwordController.text;

                    widget.onConfirm(price, amount, password, type);
                  })),
        ],
      ),
    );
  }

  onCancel() {
    Navigator.of(context).pop();
  }
}
